package com.ja.openchat.ui.chat;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.ja.openchat.BR;
import com.ja.openchat.R;
import com.ja.openchat.ui.base.JaBaseActivity;
import com.zy.openchat.App;
import com.zy.openchat.Constants;
import com.zy.openchat.core.manager.ChatExCacheManager;
import com.zy.openchat.core.model.message.ChatInfo;
import com.zy.openchat.util.CaptureStrategy;
import com.zy.openchat.util.MediaStoreCompat;
import com.zy.openchat.util.MediaUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class SettingBackgroudActivity extends JaBaseActivity {
;

    public static void start(Activity activity) {
        start(activity,null);
    }

    public static void start(Activity activity, ChatInfo chat) {
        Intent intent = new Intent();
        intent.setClass(activity, SettingBackgroudActivity.class);
        intent.putExtra("chat", chat);
        activity.startActivity(intent);
    }

    @BindView(R.id.bgAllBtn)
    View bgAllBtn;



    private ChatInfo chatInfo;

    @Override
    public void initData() {
        if (getIntent() != null) {
            chatInfo = getIntent().getParcelableExtra("chat");
        }

        if(chatInfo==null){
            setTitle(getString(R.string.chat_background));
            bgAllBtn.setVisibility(View.VISIBLE);
        }else{
            setTitle(getString(R.string.set_current_chat_bg));
            bgAllBtn.setVisibility(View.GONE);
        }
        doButtonClick();
    }

    public void setTitle(String title) {
        TextView titleText = findViewById(R.id.tv_bar_title);
        if (titleText != null) {
            titleText.setText(title);
        }
    }
    protected boolean isHaveBackButton() {
        return true;
    }

    private void doButtonClick() {
        View backView = findViewById(R.id.iv_bar_back);
        if (backView != null) {
            if (isHaveBackButton()) {
                backView.setVisibility(View.VISIBLE);
                backView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });
            } else {
                backView.setVisibility(View.GONE);
            }
        }
    }

    public static final int REQUEST_CHOOSE_CODE = 1011;
    public static final int REQUEST_CAMERA_CODE = 1012;

    private MediaStoreCompat mediaStoreCompat;

    @OnClick({R.id.chooseBgLayout, R.id.albumBgLayout, R.id.cameraBgLayout,R.id.bgAllBtn})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.chooseBgLayout:
                startActivityForResult(new Intent(this,ChooseBackgoundActivity.class),REQUEST_CHOOSE_CODE);
                break;
            case R.id.albumBgLayout:
                MediaUtils.choosePhoto(this,true);
                break;
            case R.id.cameraBgLayout:
                if(mediaStoreCompat==null){
                    mediaStoreCompat = new MediaStoreCompat(this);
                }
                mediaStoreCompat.setCaptureStrategy(new CaptureStrategy(true, App.getInstanse().getPackageName() + ".utilcode.fileprovider", Constants.userImagePath()));
                startActivityForResult(mediaStoreCompat.dispatchCaptureImageIntent(this),REQUEST_CAMERA_CODE);
                break;
            case R.id.bgAllBtn:
                ChatExCacheManager.getChatExCacheManager().applyGlobalBgToAllChatView();
                ToastUtils.showShort(getString(R.string.all_chat_bg_success));
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == REQUEST_CHOOSE_CODE&&resultCode==Activity.RESULT_OK){
            String result = data.getStringExtra(ChooseBackgoundActivity.RESULT);
            if(chatInfo!=null){
                if(!TextUtils.isEmpty(result)){
                    ChatExCacheManager.getChatExCacheManager().setChatBg(chatInfo.id,result);
                }else{
                    ChatExCacheManager.getChatExCacheManager().cleanChatBgWithChatId(chatInfo.id);
                }
                ChatActivity.startActivity(this,chatInfo);
                finish();
            }else{
                if(!TextUtils.isEmpty(result)){
                    ChatExCacheManager.getChatExCacheManager().setChatBg(ChatExCacheManager.CHAT_GLOBAL_ID,result);
                }else{
                    ChatExCacheManager.getChatExCacheManager().cleanChatBgWithChatId(ChatExCacheManager.CHAT_GLOBAL_ID);
                }
            }

        }else if(requestCode == REQUEST_CAMERA_CODE&&resultCode==Activity.RESULT_OK){
            String imagePath = mediaStoreCompat.onActivityResultForImage(data);
            if(!TextUtils.isEmpty(imagePath)){
                if(chatInfo!=null) {
                    ChatExCacheManager.getChatExCacheManager().setChatBg(chatInfo.id, imagePath);
                    ChatActivity.startActivity(this, chatInfo);
                    finish();
                }else{
                    ChatExCacheManager.getChatExCacheManager().setChatBg(ChatExCacheManager.CHAT_GLOBAL_ID,imagePath);
                }
            }
        }else{
            List<String> albumPhotos = MediaUtils.getResultAlbumData(this,requestCode,resultCode,data);
            if(albumPhotos!=null&&albumPhotos.size()>0){
                if(chatInfo!=null){
                    ChatExCacheManager.getChatExCacheManager().setChatBg(chatInfo.id,albumPhotos.get(0));
                    ChatActivity.startActivity(this,chatInfo);
                    finish();
                }else{
                    ChatExCacheManager.getChatExCacheManager().setChatBg(ChatExCacheManager.CHAT_GLOBAL_ID,albumPhotos.get(0));
                }
            }
        }
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_setting_backgound;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    protected boolean isHaveButterKnife() {
        return true;
    }


}
